#!/bin/sh

APP_INSTALL_NAME="framepack"
APP_TARGET_DIR_NAME="framepack"
CONDA_ENV_NAME=$APP_INSTALL_NAME
CONDA_PREFIX="./venv"
echo "lmdprogress:10"

# stop docker container
CONTAINER_NAME_1="framepack-download-models-temp"
docker stop $CONTAINER_NAME_1 || true
docker rm $CONTAINER_NAME_1 || true


echo "lmdprogress:20"

# stop docker container
CONTAINER_NAME_2="framepack-lmdown-temp"
docker stop $CONTAINER_NAME_2 || true
docker rm $$CONTAINER_NAME_2 || true

echo "lmdprogress:40"

# remove docker image
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/docker_image_util.sh"
gen_image_repo_url $_DOCKER_IMAGE_REPO $_DOCKER_APP_VERSION
APP_IMAGE_REPO=$APP_IMAGE_REPO_RESULT
echo "docker image repo: "$APP_IMAGE_REPO

docker rmi $APP_IMAGE_REPO


export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH


if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist $CURRENT_APP_PATH"
    echo "lmdprogress:100"
fi

echo "lmdprogress:80"

cd "$CURRENT_APP_PATH"

if [ -d "$CURRENT_APP_PATH" ]; then
    # remove models
        
    if [ "$#" -eq 0 ]; then
        MODELS_DIR="./hf_download"
        echo 'remove models'
        echo "App Dir: "$CURRENT_APP_PATH
        rm -rf $MODELS_DIR
    elif [ "$#" -eq 1 ] && [ "$1" = "keepmodels" ]; then
        #  "keepmodels" : don't delete models
        echo 'keep models'
    fi
fi

echo "lmdprogress:100"
